package cn.itbox.example.application.test;


import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Objects;

/**
 * @BelongProject springboot-study
 * @BelongPackage cn.itbox.example.application.test
 * @Description TODO
 * @ClassName NetNameAndIPPrint
 * @Author huyanbing
 * @Date 2024/3/14 15:06
 * @Version 1.0.0
 */
public class NetNameAndIPPrint {


    public static void main(String[] args) {
        try {
            // 获取所有网络接口
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                // 确保网络接口已经启用
                if (networkInterface.isUp()) {
                    // 获取网络接口名称
                    String name = networkInterface.getName();
                    System.out.println("网卡名称: " + name);

                    String displayName = networkInterface.getDisplayName();
                    System.out.println("网卡显示名称:" + displayName);




                    // 获取网络接口绑定的所有IP地址
                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress inetAddress = addresses.nextElement();
                        // 确保不是回环地址
                        if (!inetAddress.isLoopbackAddress()) {
                            // 打印IP地址
                            System.out.println("IP地址: " + inetAddress.getHostAddress());
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //DUBBO_PREFERRED_NETWORK_INTERFACE
    //dubbo.network.interface.preferred
    public static boolean isPreferredNetworkInterface(NetworkInterface networkInterface) {
        String preferredNetworkInterface = "TAP-Windows Adapter V9";
        return Objects.equals(networkInterface.getDisplayName(), preferredNetworkInterface);
    }

}
